﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using MvcEngine.Core;

using MvcEngine.Mvc.Models.ViewModels;
using MvcEngine.Core.Repositories;
using MvcEngine.Core.Utils;
using MvcEngine.Mvc.Filters;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Core.Services;
using MvcEngine.Core.Content;
using MvcEngine.Mvc.Controllers;

namespace MvcEngine.Mvc.BackOffice.Controllers
{
    public class ModuleController : BaseController
    {
        public MvcEngineModel List()
        {
            IList<Module> modules = IoC.Resolve<IModuleRepository>().GetAll();
            return new MvcEngineModelList<Module>()
            {
                List = modules
            };
        }

        public MvcEngineModel New()
        {
            return new MvcEngineModel();
        }

        public ActionResult Install(HttpPostedFileBase moduleArchive)
        {
            try
            {
                using (IUnitOfWork unitOfWork = UnitOfWorkHelper.Get())
                {
                    IModuleInstallationService service = IoC.Resolve<IModuleInstallationService>();
                    service.Install(moduleArchive);
                    unitOfWork.Commit();
                }
            }
            catch (ZipException e)
            {
#warning User NavigateHelper to redirect to error page
                //ExceptionUtils.RedirectToErrorPage(e, "Error occured while installing module", RedirectToAction("Index"));
            }
            return RedirectToAction("Index");
        }

        public ActionResult UnInstall(Guid id)
        {
            IModuleInstallationService service = IoC.Resolve<IModuleInstallationService>();
            service.UnInstall(id);
            return RedirectToAction("Index");
        }

        //[AcceptVerbs(HttpVerbs.Get)]
        //public MvcEngineModel Settings(int ctrId)
        //{
        //    PageContent pageContent = ContentManager.Current.GetPageContent(ctrId);
        //    Control control = ContentManager.Current.GetControl(pageContent.ControlId);
        //    BaseController ctrl = (BaseController)IoC.Resolve(Type.GetType(control.ControllerType));

        //    Dictionary<string, object> values = new Dictionary<string, object>();
        //    values["ctrId"] = ctrId;
        //    values["moduleName"] = ContentManager.Current.GetModule(control.ModuleId).Name;

        //    Action action = ctrl.GetResult(this, "Settings", false, values);

        //    return new MvcEngineModelItem<Action>() { ViewName = "ModuleSettings", Item = action };
        //}

        //[AcceptVerbs(HttpVerbs.Post)]
        //[ActionName("Settings")]
        //public object SaveSettings(int ctrId)
        //{
        //    PageContent pageContent = ContentManager.Current.GetPageContent(ctrId);
        //    Control control = ContentManager.Current.GetControl(pageContent.ControlId);
        //    BaseController ctrl = (BaseController)IoC.Resolve(Type.GetType(control.ControllerType));

        //    Dictionary<string, object> values = new Dictionary<string, object>();
        //    values["ctrId"] = ctrId;
        //    values["moduleName"] = ContentManager.Current.GetModule(control.ModuleId).Name;

        //    Action action = ctrl.GetResult(this, "Settings", false, values);
        //    action();

        //    return Redirect(Url.Page());
        //}
    }
}
